package com.example.hellokotlin

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.AttributeSet
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import android.util.Log
import android.widget.Button
import android.widget.ScrollView
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    companion object{
        val LOW_HAHA = 1
    }

    var started = false
    var number = 0
    var tries = 0

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        fetchSavedInstanceData(savedInstanceState)
        doGuess.setEnabled(started)
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        putInstanceData(outState)
    }

    fun start(v: View) {
        log("Game started")
        num.setText("")
        started=true
        doGuess.setEnabled(true)
        status.text=getString(R.string.guess_hint, LOW_HAHA,7)
        number =1+Math.floor(Math.random()*7).toInt()
        tries = 0
    }

    fun guess(v:View){
        if(num.text.toString()=="")return
        tries++
        log("Guessed ${num.text}(tries:${tries})")
        val g=num.text.toString().toInt()
        if(g<number){
            status.setText(R.string.status_too_low)
            num.setText("")
        }else if(g>number){
            status.setText(R.string.status_too_high)
            num.setText("")
        }else{
            status.text=getString(R.string.status_hint,tries)
            started=false
            doGuess.setEnabled(false)
        }
    }

    private fun putInstanceData(outState:Bundle?){
        if(outState !=null) with(outState){
            putBoolean("started",started)
            putInt("number",number)
            putInt("tries",tries)
            putString("statusMsg",status.text.toString())
            putStringArrayList("logs", ArrayList(console.text.split("\n")))
        }

    }

    private fun fetchSavedInstanceData(
        savedInstanceState: Bundle?
    ) {
        if (savedInstanceState != null)
            with(savedInstanceState) {
                started = getBoolean("started")
                number = getInt("tries")
                status.text = getString("statusMsg")
                console.text = getStringArrayList("logs")!!
                    .joinToString("\n")
            }

    }


    private fun log(msg: String) {
        Log.d("LOG", msg)
        console.log(msg)
    }
}


class Console(ctx: Context, aset: AttributeSet? = null) : ScrollView(ctx, aset) {
    val tv = TextView(ctx)
    var text: String
        get() = tv.text.toString()
        set(value) {
            tv.setText(value)
        }

    init {
        setBackgroundColor(0x40FFFF00)
        addView(tv)
    }

    fun log(msg: String) {
        val l = tv.text.let {
            if (it == "") listOf() else it.split("\n")
        }.takeLast(100) + msg

        tv.text = l.joinToString("\n")

        post(object : Runnable {
            override fun run() {
                fullScroll(FOCUS_DOWN)
            }
        })

    }
}